<template>
  <component is="a-modal" v-model:visible="visible" :width="1000" :title="title" :mask-closable="false"
    :ok-loading="loading" @cancel="close" @before-ok="submit">
    <!-- 表单信息 start -->
     <div style="max-height: 70vh;overflow: auto;">
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item field="cate_id" label="分类">
        <a-select :fieldNames="{ value: 'id', label: 'name' }" v-model="formData.cate_id" placeholder="请选择"
          :allow-clear="true">
          <a-option v-for="(item, index) of paramsData.cate" :value="item.id" :label="item.name" />
        </a-select>
      </a-form-item>
      <a-form-item label="中文简体标题" field="title">
        <a-input v-model="formData.title" placeholder="请输入" />
      </a-form-item>
    
      <a-form-item label="排序" field="sort">
        <a-input-number mode="button" v-model="formData.sort" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="状态" field="state">
        <sa-radio :data="ENUMS.STATE" v-model="formData.state" placeholder="请选择"></sa-radio>
      </a-form-item>
      <a-form-item label="背景图">
        <sa-upload-image v-model="formData.img" :limit="1"></sa-upload-image>
      </a-form-item>
      <a-form-item label="描述" field="desc">
        <a-textarea :auto-size="{ minRows:2,maxRows:5}" v-model="formData.desc" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="详情" field="content">
        <ma-wangEditor :height="400" v-model="formData.content"></ma-wangEditor>
      </a-form-item>


    </a-form>
  </div>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed, watch, nextTick } from 'vue'
import { Message } from '@arco-design/web-vue'
import noticeApi from '@/api/message/notice'
import SaRadio from '@cps/sa-radio/index.vue'
import SaUploadImage from '@cps/sa-upload-image/index.vue'
 import MaWangEditor from '@cps/ma-wangEditor/index.vue'
import ENUMS from '@/utils/enums'
import tool from '@/utils/tool';
import { throttle } from 'lodash'
const paramsData = ref([])
const getParams = async (id) => {
  const { data } = await noticeApi.addNoticeParams()
  paramsData.value = { ...data }
  paramsData.value.area = tool.simpleTransformArray(data.area)
}
const emit = defineEmits(['success'])
// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)
let title = computed(() => {
  return (mode.value == 'add' ? '新增' : '编辑')
})
// 表单信息
const formData = reactive({
  cate_id: 1,
  state: 1,
  sort: 0,
  file: '',
  img: '',
  desc: '',
  title: '',
  id: '',
  content: ''
})
// 验证规则
const rules = ref({
  area: [{ required: true, message: '请选择' }],
  state: [{ required: true, message: '请选择' }],
  cate_id: [{ required: true, message: '请选择' }],
  title: [{ required: true, message: '请输入' }],
  content: [{ required: true, message: '请输入' }],
  sort: [{ required: true, message: '请输入' }],
});
// 打开弹框
const open = async (type = 'add', id = '') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
  await getParams()
  if (type == 'add') {

  } else {
    formData.id = id
  }

}
// 初始化页面数据
const initPage = async () => {
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
      if (key == 'data') {
        formData['video_url'] = data[key]
      }
    }
  }
  console.log('111', formData);
}

// 数据保存
const submit =async (done) => {
  console.log('111',formData); 
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    try {
      let data = { ...formData }
      let result = {}
      if (mode.value === 'add') {
        // 添加数据
        data.id = ''
        result = await noticeApi.addNotice(data)
      } else {
        // 修改数据
        result = await noticeApi.updateNotice(data)
      }
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    } catch {
      loading.value = false
    }
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
